import { getSession } from '@/lib/auth';
import { db } from '@/lib/db';
import { baseProcedure, createTRPCRouter } from '@/lib/trpc/init';
import { BizError, ErrorCode } from '@/service/error';

export const address = createTRPCRouter({
  listAllAddresses: baseProcedure.query(async () => {
    const session = await getSession();
    if (!session?.user)
      throw new BizError(ErrorCode.Unauthorized, '用户未登录');
    return await db.query.address.findMany({
      where: (address, { eq }) => eq(address.userId, session.user.id),
    });
  }),
});
